package com.yonyong.tao.controller;

import com.yonyong.tao.commons.api.receive.AutoParam;
import com.yonyong.tao.commons.api.receive.PageParam;
import com.yonyong.tao.commons.api.vo.Result;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.text.MessageFormat;

/**
 * @Describtion spring valid
 * @Author yonyong
 * @Date 2020/3/31 15:55
 * @Version 1.0.0
 **/
@RestController
@RequestMapping(value = "/valid",produces = "application/json;charset=utf-8")
@Api(tags="spring valid")
@ApiSort(value = 1)
@Validated
@Slf4j
public class ValidController {

    @GetMapping("validated/{id}")
    @ApiOperation(value = "方法校验",notes = "",position = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "管理员name",paramType = "query")
    })
    public Result<?> validated(@PathVariable final String id,@NotBlank(message = "管理员name不能为空")final String name){
        return Result.ok(MessageFormat.format("id:{0},name:{1}",id,name));
    }

    @GetMapping("valid/{id}")
    @ApiOperation(value = "实体校验",notes = "",position = 2)
    public Result<?> valid(@PathVariable final String id, @Valid final PageParam pageParam){
        return Result.ok(MessageFormat.format("id:{0},pageParam:{1}",id,pageParam.toString()));
    }

    @GetMapping("auto/{id}")
    @ApiOperation(value = "自定义校验注解",notes = "",position = 3)
    public Result<?> autoConfig(@PathVariable final String id, @Valid final AutoParam autoParam){
        return Result.ok(MessageFormat.format("id:{0},autoParam:{1}",id,autoParam.toString()));
    }
}
